//
//  news.m
//  ios_news
//
//  Created by Han on 14-6-11.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "Market.h"

@implementation Market


- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.title  = [attributes valueForKeyPath:@"title"];
    self.prevClose = [attributes valueForKeyPath:@"prevClose"];
    self.open = [attributes valueForKeyPath:@"open"];
    
    return self;
}


+ (NSURLSessionDataTask *)getDataWithBlock:(void (^)(NSArray *posts, NSError *error))block :(NSString *) url :(NSDictionary *)params {
    
    AFHTTPSessionManager *sharedClient = [[AFHTTPSessionManager alloc] init];
    sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    
    
    return [sharedClient GET:url parameters:params success:^(NSURLSessionDataTask * __unused task, id JSON) {
        
        NSDictionary *responseData = JSON;  // [JSON valueForKeyPath:@"data"]
        
        responseData = [responseData objectForKey:@"results"];
        
        
        NSMutableArray *mutableNews = [NSMutableArray arrayWithCapacity:[responseData count]];
        for (NSDictionary *attributes in responseData) {
            Market *news = [[Market alloc] initWithAttributes:attributes];
            [mutableNews addObject:news];
        }
        
        if (block) {
            block([NSArray arrayWithArray:mutableNews], nil);
        }
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}

@end
